#! /usr/bin/env python
# coding=utf-8
import copy
import os
import argparse
import sys
from argparse import RawTextHelpFormatter


parser = argparse.ArgumentParser(
    description='''
    运行 minibar.py 进行demultiplex
    运行方法
    run_minibar.py -q CCS_test.fq -i MinIndexCombinationPeperomonia.txt -o result

    ''',formatter_class=RawTextHelpFormatter)


parser.add_argument('-q',
                help='三代测序获得的fastq文件')

parser.add_argument('-i',
                help='包含每个样本引物tag信息的index 文件,为MinIndexCombinationPeperomonia.txt, 可以在程序中找到')

parser.add_argument('-o',
                help='输出路径')

args = parser.parse_args()


if not args.q or not args.i or not args.o:
    parser.print_help()
    sys.exit()


fastq_file = args.q#r'CCS_test.fq'

index_file = args.i# #r'MinIndexCombinationPeperomonia.txt'

outfile = args.o

import os
import time
#import shutil
tmp_file = 'tmp_'+str(int(time.time()))


#print(tmp_file)

os.system('mkdir %s' % tmp_file)
os.system('minibar.py %s %s  -C -T -P %s/ -S -M 1 -p 0.9 -F -S > left%s' % (index_file, fastq_file , tmp_file,tmp_file))
os.system('rm %s/Multiple_Matches.fastq' % tmp_file)
os.system('rm %s/unk.fastq' % tmp_file)
os.system('cat %s/*.fastq left%s > %s' %(tmp_file,tmp_file,tmp_file+'all.fq'))
os.system('barcode_assign_to_file.py -i %s -o %s' %(tmp_file+'all.fq', outfile))
#os.system('')
# 	minibar.py MinIndexCombinationPeperomonia.txt  -C -T -P result/ -S -M 1 -p 0.9 -F -S > left.out
# 	rm result/Multiple_Matches.fastq
# 	rm result/unk.fastq
# 	cat result/*.fastq left.out > result.fq
# 	mv result result_bac
# 	barcode_assign_to_file.py -i result.fq -o result